From 3beb9ea381fa5c02fddf2f5a399522792d65fb62 Mon Sep 17 00:00:00 2001 From: tsteven4 Date: Sat, 27 Sep 2014 15:31:49 +0000 Subject: [PATCH] supply a US-ASCII QTextCodec. --- gpsbabel/Makefile.in | 16 +++-- gpsbabel/main.cc | 18 ++--- gpsbabel/src/core/usasciicodec.cc | 107 ++++++++++++++++++++++++++++++ gpsbabel/src/core/usasciicodec.h | 39 +++++++++++ 4 files changed, 165 insertions(+), 15 deletions(-) create mode 100644 gpsbabel/src/core/usasciicodec.cc create mode 100644 gpsbabel/src/core/usasciicodec.h diff --git a/gpsbabel/Makefile.in b/gpsbabel/Makefile.in index 751a9417f..8c6c643a3 100644 --- a/gpsbabel/Makefile.in +++ b/gpsbabel/Makefile.in @@ -111,6 +111,7 @@ LIBOBJS = queue.o route.o waypt.o filter_vecs.o util.o vecs.o mkshort.o \ formspec.o xmltag.o cet.o cet_util.o fatal.o rgbcolors.o \ inifile.o garmin_fs.o gbsleep.o units.o @GBSER@ gbser.o \ gbfile.o parse.o session.o src/core/xmlstreamwriter.o \ + src/core/usasciicodec.o \ $(PALM_DB) $(GARMIN) $(JEEPS) $(SHAPE) @ZLIB@ $(FMTS) $(FILTERS) OBJS = main.o globals.o $(LIBOBJS) @FILEINFO@ @@ -444,7 +445,7 @@ cet.o: cet.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h gbfile.h \ cet.h cet_util.h inifile.h session.h src/core/datetime.h cet_util.o: cet_util.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h \ gbfile.h cet.h cet_util.h inifile.h session.h src/core/datetime.h \ - cet/ansi_x3_4_1968.h cet/cp1252.h + cet/ansi_x3_4_1968.h cet/cp1252.h cet/iso_8859_8.h compegps.o: compegps.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h \ gbfile.h cet.h cet_util.h inifile.h session.h src/core/datetime.h \ csv_util.h jeeps/gpsmath.h jeeps/gpsport.h @@ -452,11 +453,11 @@ cst.o: cst.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h gbfile.h \ cet.h cet_util.h inifile.h session.h src/core/datetime.h strptime.h csv_util.o: csv_util.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h \ gbfile.h cet.h cet_util.h inifile.h session.h src/core/datetime.h \ - csv_util.h grtcirc.h strptime.h jeeps/gpsmath.h jeeps/gpsport.h \ - garmin_fs.h jeeps/gps.h jeeps/gpsdevice.h jeeps/gpssend.h \ - jeeps/gpsread.h jeeps/gpsutil.h jeeps/gpsapp.h jeeps/gpsprot.h \ - jeeps/gpscom.h jeeps/gpsfmt.h jeeps/gpsmem.h jeeps/gpsrqst.h \ - jeeps/gpsinput.h jeeps/gpsproj.h xcsv_tokens.gperf + csv_util.h garmin_fs.h jeeps/gps.h jeeps/gpsport.h jeeps/gpsdevice.h \ + jeeps/gpssend.h jeeps/gpsread.h jeeps/gpsutil.h jeeps/gpsapp.h \ + jeeps/gpsprot.h jeeps/gpscom.h jeeps/gpsfmt.h jeeps/gpsmath.h \ + jeeps/gpsmem.h jeeps/gpsrqst.h jeeps/gpsinput.h jeeps/gpsproj.h \ + grtcirc.h src/core/logging.h strptime.h xcsv_tokens.gperf delbin.o: delbin.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h \ gbfile.h cet.h cet_util.h inifile.h session.h src/core/datetime.h \ src/core/xmltag.h @@ -795,7 +796,7 @@ magproto.o: magproto.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h \ magellan.h gbser.h explorist_ini.h main.o: main.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h gbfile.h \ cet.h cet_util.h inifile.h session.h src/core/datetime.h filterdefs.h \ - csv_util.h + csv_util.h src/core/usasciicodec.h mapasia.o: mapasia.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h \ gbfile.h cet.h cet_util.h inifile.h session.h src/core/datetime.h mapbar_track.o: mapbar_track.cc defs.h config.h queue.h zlib/zlib.h \ @@ -919,6 +920,7 @@ smplrout.o: smplrout.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h \ filterdefs.h grtcirc.h sort.o: sort.cc defs.h config.h queue.h zlib/zlib.h zlib/zconf.h gbfile.h \ cet.h cet_util.h inifile.h session.h src/core/datetime.h filterdefs.h +src/core/usasciicodec.o: src/core/usasciicodec.cc src/core/usasciicodec.h src/core/xmlstreamwriter.o: src/core/xmlstreamwriter.cc \ src/core/xmlstreamwriter.h stackfilter.o: stackfilter.cc defs.h config.h queue.h zlib/zlib.h \ diff --git a/gpsbabel/main.cc b/gpsbabel/main.cc index 2cbf9507a..59eb24cae 100644 --- a/gpsbabel/main.cc +++ b/gpsbabel/main.cc @@ -26,6 +26,7 @@ #include "csv_util.h" #include "inifile.h" #include "session.h" +#include "src/core/usasciicodec.h" #include #include @@ -41,7 +42,7 @@ typedef struct arg_stack_s { } arg_stack_t; static arg_stack_t -*push_args(arg_stack_t* stack, const int argn, const int argc, char* argv[]) +* push_args(arg_stack_t* stack, const int argn, const int argc, char* argv[]) { arg_stack_t* res = (arg_stack_t*) xmalloc(sizeof(*res)); @@ -54,7 +55,7 @@ static arg_stack_t } static arg_stack_t -*pop_args(arg_stack_t* stack, int* argn, int* argc, char** argv[]) +* pop_args(arg_stack_t* stack, int* argn, int* argc, char** argv[]) { arg_stack_t* res; char** argv2 = *argv; @@ -238,10 +239,11 @@ main(int argc, char* argv[]) queue* wpt_head_bak, *rte_head_bak, *trk_head_bak; /* #ifdef UTF8_SUPPORT */ signed int wpt_ct_bak, rte_ct_bak, trk_ct_bak; /* #ifdef UTF8_SUPPORT */ arg_stack_t* arg_stack = NULL; + (void) new gpsbabel::UsAsciiCodec(); /* make sure a US-ASCII codec is available */ #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) - // Qt 5.0 uses QString::fromUtf8 to convert from character pointers - // and QBytreArrays to QStrings while previous version of Qt used + // Qt 5.0 uses QString::fromUtf8 to convert from character pointers + // and QBytreArrays to QStrings while previous version of Qt used // QString::fromAscii. QString::fromAscii used the codec set // by QTextCode::setCodecForCStrings. // This makes the converstion consistent between Qt4 and Qt5. @@ -320,10 +322,10 @@ main(int argc, char* argv[]) } switch (c) { - //case 'c': - // optarg = argv[argn][2] ? argv[argn]+2 : argv[++argn]; - // cet_convert_init(optarg, 1); - // break; + //case 'c': + // optarg = argv[argn][2] ? argv[argn]+2 : argv[++argn]; + // cet_convert_init(optarg, 1); + // break; case 'i': optarg = argv[argn][2] ? argv[argn]+2 : argv[++argn]; diff --git a/gpsbabel/src/core/usasciicodec.cc b/gpsbabel/src/core/usasciicodec.cc new file mode 100644 index 000000000..0654d96d4 --- /dev/null +++ b/gpsbabel/src/core/usasciicodec.cc @@ -0,0 +1,107 @@ +/* + Copyright (C) 2014 Robert Lipe, gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA + + */ + +#include "usasciicodec.h" +#include +#include +#include +#include +#include + +// The availability of a US-ASCII codec is not promised by QTextCodec. +// On Mac OS X is is likely one will not be available. +// We provide our own to guarantee we have one. + +namespace gpsbabel +{ + +QByteArray UsAsciiCodec::name() const +{ + return "US-ASCII"; +} + +QList UsAsciiCodec::aliases() const +{ + QList list; + list += "iso-ir-6"; + list += "ANSI_X3.4-1968"; + list += "ANSI_X3.4-1986"; + list += "ISO_646.irv:1991"; + list += "ISO646-US"; + list += "us"; + list += "IBM367"; + list += "cp367"; + list += "csASCII"; + return list; +} + +int UsAsciiCodec::mibEnum() const +{ + return 3; +} + +QString UsAsciiCodec::convertToUnicode(const char* chars, int len, ConverterState* state) const +{ + QString result(len, Qt::Uninitialized); + QChar* uc = result.data(); + const unsigned char* c = (const unsigned char*)chars; + int invalid = 0; + + for (int i = 0; i < len; i++) { + if (c[i] < 128) { + uc[i] = QLatin1Char(c[i]); + } else { + uc[i] = QChar::ReplacementCharacter; + ++invalid; + } + } + + if (state) { + state->invalidChars += invalid; + state->remainingChars = 0; + } + return result; +} + +QByteArray UsAsciiCodec::convertFromUnicode(const QChar* uc, int len, ConverterState* state) const +{ + QByteArray result(len, Qt::Uninitialized); + unsigned char* c = (unsigned char*)result.data(); + const char replacement = (state && state->flags & ConvertInvalidToNull) ? 0 : '?'; + int invalid = 0; + int u; + + for (int i = 0; i < len; i++) { + u = uc[i].unicode(); + if (u < 128) { + c[i] = (char) u; + } else { + c[i] = replacement; + ++invalid; + } + } + + if (state) { + state->invalidChars += invalid; + state->remainingChars = 0; + } + return result; +} + +} // namespace gpsbabel diff --git a/gpsbabel/src/core/usasciicodec.h b/gpsbabel/src/core/usasciicodec.h new file mode 100644 index 000000000..602bb6ec6 --- /dev/null +++ b/gpsbabel/src/core/usasciicodec.h @@ -0,0 +1,39 @@ +/* + Copyright (C) 2014 Robert Lipe, gpsbabel.org + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA + + */ + +#ifndef QUSASCIICODEC_H +#define QUSASCIICODEC_H + +#include + +namespace gpsbabel +{ + +class UsAsciiCodec : public QTextCodec +{ +public: + QByteArray name() const; + QList aliases() const; + int mibEnum() const; + QString convertToUnicode(const char*, int, ConverterState*) const; + QByteArray convertFromUnicode(const QChar*, int, ConverterState*) const; +}; + +} // namespace gpsbabel +#endif // QUSASCIICODEC_H -- 2.30.2